package com.yjd.pub.doctor.service;

import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.IntroDetailModel;
import com.yjd.comm.base.model.PagerModel;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.doctor.model.DoctorModel;
import com.yjd.comm.doctor.service.IDoctorService;
import com.yjd.comm.enums.ReferenceTypeEnum;
import com.yjd.comm.org.model.OrganizationModel;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.tag.model.TagInfoDto;
import com.yjd.comm.tag.service.ITagRelevanceService;
import com.yjd.comm.tag.service.ITagService;
import com.yjd.comm.util.CityUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ResultMapBuilder;
import com.yjd.pub.base.dao.IIntroDetailDao;
import com.yjd.pub.doctor.dao.IDoctorDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <b>description</b>：医生业务实现<br>
 * <b>time</b>：2017-06-27 08:52:14 <br>
 * <b>author</b>：  yangchangyan@yijiedai.com
 */
@Service("doctorService")
public class DoctorServiceImpl extends BaseServiceImpl implements IDoctorService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public DoctorModel insert(DoctorModel model) throws Exception {
		return this.doctorDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(DoctorModel model) throws Exception {
		return this.doctorDao.updateModel(model, true);
	}

	@Override
	public int createOrUpdate(DoctorModel dm, String doctorDetail, String[] tags) throws Exception {

		Long currentTime = FrameUtil.getTime();
		// 创建医生详情
		IntroDetailModel detail = new IntroDetailModel();
		detail.setContent(doctorDetail);
		detail.setRef_type(ReferenceTypeEnum.DOCTOR.getCode());
		detail.setLast_update_time(currentTime);
		if (null == dm.getDetail_id()) {
			detail.setCreate_time(currentTime);
			detail = introDetailDao.insertModel(detail, true);
		} else {
			detail.setDetail_id(dm.getDetail_id());
			introDetailDao.updateModel(detail, true);
		}

		// 创建医生基本信息
		if(null == dm.getIs_used()){
			dm.setIs_used(0); // 默认不启用
		}
		if(null == dm.getIs_spread()){
			dm.setIs_spread(0); // 默认不推广
		}
		dm.setDetail_id(detail.getDetail_id());
		dm.setLast_update_time(currentTime);
		if (null == dm.getDoctor_id()) {
			dm.setCreate_time(currentTime);
			dm = doctorDao.insertModel(dm, true);
		} else {
			doctorDao.updateModel(dm, true);
		}

		if(null != tags && tags.length > 0) {
			tagRelevanceService.updateTagRelevance(tags, dm.getDoctor_id(), ReferenceTypeEnum.DOCTOR);
		}

		return 1;
	}

	/**
	 * 根据id删除
	 * 
	 * @param doctor_id
	 * @return
	 * @throws Exception
	 */
	public int delete(long doctor_id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("doctor_id", doctor_id);
		return this.doctorDao.deleteModel(paramMap, true);
	}

	/**
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@Override
	public PagerModel getDoctorListByKeys(Map<String, Object> param) throws Exception {
		return this.getPageModel(param,"getDoctorListByKeys", DbWREnums.READ);
	}

	@Override
	public List<Map<String, Object>> queryDoctor(Map<String, Object> queryParamMap) throws Exception {
		queryParamMap.put("is_used", "1");
		List<Map<String, Object>> doctors = new ArrayList<>();
		PagerModel projectPage = this.getPageModel(queryParamMap, DbWREnums.READ);
		for(Object obj : projectPage.getDataList()){
			doctors.add(this.transformToMap( (DoctorModel)obj ));
		}
		return doctors;
	}

	@Override
	public Map<Long, String> queryDoctorName(Long org_id) throws Exception {
		Map<Long, String> doctorIds = new HashMap<Long, String>();
		List<DoctorModel> doctors =null;
		if(org_id == 0){
			doctors = doctorDao.getModelList(FrameUtil.newHashMap("is_used", "1"), DbWREnums.READ.isWrite());
		}else{
			doctors = doctorDao.getModelList(FrameUtil.newHashMap("org_id", org_id, "is_used", "1"), DbWREnums.READ.isWrite());
		}

		for(DoctorModel doctor:doctors){
			doctorIds.put(doctor.getDoctor_id(), doctor.getName());
		}

		return doctorIds;
	}

	private Map<String, Object> transformToMap(DoctorModel dm) throws Exception {
		Map<String, Object> doctorResult = ResultMapBuilder.build(dm);

		OrganizationModel org = organizationService.getModelById(dm.getOrg_id(), DbWREnums.READ);
		doctorResult.put("org_name", null == org ? "":org.getOrg_name());

		doctorResult.put("org_address", null == org ? "": CityUtil.getFullCityNames(org.getOrg_city(), "") + org.getOrg_address());

		// 查找相关联的tag
		List<Long> refIdList  = new ArrayList<Long>();
		refIdList.add(dm.getDoctor_id());
		List<TagInfoDto> allTagList = tagService.getTagNameList(FrameUtil.newHashMap("ref_type", ReferenceTypeEnum.DOCTOR.getCode() ,"idList",refIdList));
		List<String> tags = new ArrayList<String>();
		for(TagInfoDto t:allTagList){
			tags.add(t.getTag_name());
		}
		doctorResult.put("tags", tags);

		return doctorResult;
	}

	@Override
	public int updateMap(Map param) throws Exception {
		return doctorDao.updateByMap(param,true);
	}

	@Resource
	private ITagRelevanceService tagRelevanceService;

	@Resource
	private IIntroDetailDao introDetailDao;

	@Resource
	private IOrganizationService organizationService;

	@Resource
	private ITagService tagService;

	@Resource
	private IDoctorDao doctorDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.doctorDao;
	}

}
